import Vue from "vue"
import Router from "vue-router"
import routes from "./common/config/router.js"

Vue.use(Router)
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const router = new Router({routes})
//全局前置守卫
router.beforeEach((to, from, next) => {
	let token = window.sessionStorage.getItem('token')
	let user = window.sessionStorage.getItem('user')
	
	if (token) {//已登录
		//防止重复登录
		if(to.path === 'login') {
			//跳回上一页
			Vue.prototype.$message('请不要重复登录')
			return next({name: from.name ? from.name : 'index'})
		}
		//其他的验证
		if (to.name !== 'error_404') {
			//超级管理员跳过验证
			if(user) {
				user = JSON.parse(user)
				if (user.super === 1) {
					return next()
				}
			}
			let rules = window.sessionStorage.getItem('rules')
			rules = rules ? JSON.parse(rules) : []
			let index = rules.findIndex(item => {
				return item.rule_id > 0 && item.desc === to.name
			})
			if (index === -1) {
				Vue.prototype.$message.error('你没有权限')
				return next({name: from.name ? from.name : 'error_404'})
			}
		}
		next()
	} else { //未登录
		//跳过登录页
		if (to.path === '/login') {
			return next()
		}
		Vue.prototype.$message.error('请先登录')
		next({path: '/login'})
	}
})
export default router